package gui;


import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import neuralnetworks.MultiLayerPerceptron;
import players.GuiPlayer;
import players.MinimaxPlayer;
import players.MobilityPlayer;
import players.NeuralNetworkPlayer;
import players.Player;
import players.PositionalPlayer;
import players.RandomPlayer;

import model.Field;

public class SettingsPanel extends JPanel
{
	private static final long serialVersionUID = 1L;

	String path = "../src/experiments/results2/";
	String[] files;
	
	private boolean input_set = false, undo = false;
	
	private JComboBox list_white, list_black;
	private JButton button, undo_button;
	private JSlider depth_input;
	private JCheckBox random_CheckBox;
	
	BoardPanel d_boardFrame;
	
	public SettingsPanel(BoardPanel boardFrame)
	{
		super();
		d_boardFrame = boardFrame;
		GridLayout grid = new GridLayout(2,2);
		grid.setHgap(10);
		grid.setVgap(10);
		BorderLayout border = new BorderLayout();
		border.setHgap(10);
		setLayout(border);
		
		depth_input = new JSlider(1, 10, 3);
		depth_input.setPaintLabels(true);
		depth_input.setPaintTicks(true);
		depth_input.setSnapToTicks(true);
		depth_input.setMajorTickSpacing(1); 
		random_CheckBox = new JCheckBox("random initialization", false);
		JPanel west = new JPanel();
			west.setLayout(grid);
			JPanel temp = new JPanel();
				temp.add(new JLabel("minimax depth: "));
				temp.add(depth_input);
			west.add(temp);
			west.add(random_CheckBox);
		add(west, BorderLayout.WEST);
		
		//set possible players and network files
		files = new String[13*10+4];
		files[0] = "Human";
		files[1] = "Random";
		files[2] = "Positional";
		files[3] = "Mobility";
		for(int expNr = 1; expNr <= 13; expNr++)
			for(int run = 0; run < 10; run++)
				files[(expNr - 1)*10 + run + 4] = "exp"+expNr+"_"+run+".m";
		
		JPanel center = new JPanel();
			center.setLayout(grid);
			list_white = new JComboBox(files);
			list_black = new JComboBox(files);
			center.add(new JLabel("black player: "));
			center.add(list_black);
			center.add(new JLabel("white player: "));
			center.add(list_white);
		add(center,BorderLayout.CENTER);
		
		button = new JButton("play");
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				play_pressed(true);
			}
		});
		undo_button = new JButton("undo");
		undo_button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				undo_pressed(true);
			}
		});
		JPanel temp2 = new JPanel();
			temp2.setLayout(new GridLayout(2,1));
			temp2.add(button);
			temp2.add(undo_button);
		add(temp2, BorderLayout.EAST);
		
		add(new JPanel(), BorderLayout.NORTH);
		add(new JPanel(), BorderLayout.SOUTH);
	}
	
	public boolean play_pressed() 
	{
		return input_set;
	}
	
	public void play_pressed(boolean set)
	{
		input_set = set;
		d_boardFrame.input_set = set;
	}
	
	public boolean undo_pressed() 
	{
		return undo;
	}
	
	public void undo_pressed(boolean set)
	{
		undo = set;
		d_boardFrame.undo_pressed = set;
	}
	
	public String getFile(byte color) 
	{
		while(!play_pressed())
			try{ //try later
				Thread.sleep(200);
			}catch(Exception e) {}
				
		return color == Field.WHITE ? files[list_white.getSelectedIndex()] : files[list_black.getSelectedIndex()];
	}

	public int getDepth() 
	{
		while(!play_pressed())
			try{ //try later
				Thread.sleep(200);
			}catch(Exception e) {}
			
		return depth_input.getValue();
	}
	
	public String getPath() 
	{
		return path;
	}

	public boolean random_initialization() 
	{	
		return random_CheckBox.isSelected();
	}
	
	public Player getPlayer(byte color) throws FileNotFoundException
	{
		String file = getFile(color);
		int depth = getDepth();
		
		Player p = null;
		
		if(file == "Human")
			return new GuiPlayer(d_boardFrame);
		else if(file == "Random")
			p = new RandomPlayer();
		else if(file == "Positional")
			p = new PositionalPlayer();
		else if(file == "Mobility")
			p = new MobilityPlayer();
		else 
			p = new NeuralNetworkPlayer(readNetworks(getPath() + file), null);
		
		return new MinimaxPlayer(p, depth);
	}
	
	public MultiLayerPerceptron[] readNetworks(String file) throws FileNotFoundException 
	{
		// read network from file
		System.out.print("trying to read " + file + " . . . ");
		Scanner sc = new Scanner(new File(file));
		int nNets = sc.nextInt();

		MultiLayerPerceptron[] nets = new MultiLayerPerceptron[nNets];
		for (int n = 0; n < nNets; n++)
			nets[n] = new MultiLayerPerceptron(sc);

		sc.close();
		System.out.println("file ok.");
		return nets;
	}
}
